package cn.iocoder.yudao.module.trade.controller.admin.oderreminder;

import cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants;
import cn.iocoder.yudao.module.trade.enums.reminder.ReminderEnum;
import com.google.common.collect.Lists;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.trade.controller.admin.oderreminder.vo.*;
import cn.iocoder.yudao.module.trade.dal.dataobject.oderreminder.OderReminderDO;
import cn.iocoder.yudao.module.trade.service.oderreminder.OderReminderService;

@Tag(name = "管理后台 - 催单")
@RestController
@RequestMapping("/trade/oder-reminder")
@Validated
public class OderReminderController {

    @Resource
    private OderReminderService oderReminderService;

    @PostMapping("/create")
    @Operation(summary = "创建催单")
    @PreAuthorize("@ss.hasPermission('trade:oder-reminder:create')")
    public CommonResult<Long> createOderReminder(@Valid @RequestBody OderReminderSaveReqVO createReqVO) {
        if (createReqVO.getOrderId() == null) {
            return CommonResult.error(ErrorCodeConstants.ORDER_NOT_FOUND);
        }
        List<OderReminderDO> reminder = oderReminderService.getOderReminderByOrderId(Lists.newArrayList(createReqVO.getOrderId()));
        if (reminder != null && reminder.size() > 0) {
            return CommonResult.error(ErrorCodeConstants.ODER_REMINDER_ORDER_EXISTS);
        } else {
            createReqVO.setType(ReminderEnum.MANUAL.getType());
            return success(oderReminderService.createOderReminder(createReqVO));
        }
    }

    @PutMapping("/update")
    @Operation(summary = "更新催单")
    @PreAuthorize("@ss.hasPermission('trade:oder-reminder:update')")
    public CommonResult<Boolean> updateOderReminder(@Valid @RequestBody OderReminderSaveReqVO updateReqVO) {
        oderReminderService.updateOderReminder(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除催单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('trade:oder-reminder:delete')")
    public CommonResult<Boolean> deleteOderReminder(@RequestParam("id") Long id) {
        oderReminderService.deleteOderReminder(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得催单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('trade:oder-reminder:query')")
    public CommonResult<OderReminderRespVO> getOderReminder(@RequestParam("id") Long id) {
        OderReminderDO oderReminder = oderReminderService.getOderReminder(id);
        return success(BeanUtils.toBean(oderReminder, OderReminderRespVO.class));
    }

    @PostMapping("/page")
    @Operation(summary = "获得催单分页")
    @PreAuthorize("@ss.hasPermission('trade:oder-reminder:query')")
    public CommonResult<PageResult<OderReminderRespVO>> getOderReminderPag(@RequestBody OderReminderPageReqVO pageReqVO) {
        PageResult<OderReminderRespVO> pageResult = oderReminderService.getOderReminderPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OderReminderRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出催单 Excel")
    @PreAuthorize("@ss.hasPermission('trade:oder-reminder:export')")
    @OperateLog(type = EXPORT)
    public void exportOderReminderExcel(@Valid OderReminderPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OderReminderRespVO> list = oderReminderService.getOderReminderPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "催单.xls", "数据", OderReminderRespVO.class,
                        BeanUtils.toBean(list, OderReminderRespVO.class));
    }

}